home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / d_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  6.8 KB  |  271 lines

  1. /*
  2.  * Routines to manipulate the dialing directory file pcomm.dial_dir
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "dial_dir.h"
  7. #include "param.h"
  8.  
  9. /*
  10.  * Read the dialing directory.  Returns a pointer to a static area
  11.  * containing the DIAL_DIR structure.  All of the entries are created
  12.  * regardless of the number of physical entries in the file.  Element
  13.  * number zero is reserved for the "manual" entry.  All errors are fatal.
  14.  */
  15.  
  16. struct DIAL_DIR *
  17. read_dir(extra)
  18. char *extra;
  19. {
  20.     extern char *null_ptr;
  21.     FILE *fp, *my_fopen();
  22.     int i, line, oops;
  23.     char *str_dup(), buf[200], *temp_token, *str, *str_tok(), token[20];
  24.     char message[80], *sep, *findfile();
  25.     static struct DIAL_DIR d;
  26.     void error_win();
  27.  
  28.     if ((d.d_path = findfile(extra, "pcomm.dial_dir")) == NULL)
  29.         error_win(1, "Support file \"pcomm.dial_dir\" is missing", "or no read permission");
  30.  
  31.     if (!(fp = my_fopen(d.d_path, "r"))) {
  32.         sprintf(buf, "\"%s\" for read", d.d_path);
  33.         error_win(1, "Can't open dialing directory file", buf);
  34.     }
  35.  
  36.     sep = ";;---;;\n";
  37.     line = 0;
  38.     oops = 0;
  39.     while (fgets(buf, 200, fp) != NULL) {
  40.         line++;
  41.         if (line > NUM_DIR)
  42.             break;
  43.                     /* get the token */
  44.         if (!(temp_token = str_tok(buf, '='))) {
  45.             sprintf(message, "is missing a token at line %d", line);
  46.             oops++;
  47.             break;
  48.         }
  49.         /*
  50.          * Parse the rest of the line.  This is similar to using
  51.          * the "real" strtok() function, but this version returns
  52.          * a pointer to NULL if the token is missing.  Note the use
  53.          * of the array of field separators.
  54.          */
  55.         for (i=0; i<8; i++) {
  56.             if (!(str = str_tok((char *) NULL, sep[i]))) {
  57.                 sprintf(message, "is missing a parameter at line %d", line);
  58.                 oops++;
  59.                 break;
  60.             }
  61.             switch (i) {
  62.                 case 0:
  63.                     d.name[line] = str_dup(str);
  64.                     break;
  65.                 case 1:
  66.                     d.number[line] = str_dup(str);
  67.                     break;
  68.                 case 2:
  69.                     d.baud[line] = atoi(str);
  70.                     break;
  71.                 case 3:
  72.                     d.parity[line] = *str;
  73.                     break;
  74.                 case 4:
  75.                     d.dbits[line] = atoi(str);
  76.                     break;
  77.                 case 5:
  78.                     d.sbits[line] = atoi(str);
  79.                     break;
  80.                 case 6:
  81.                     d.duplex[line] = *str;
  82.                     break;
  83.                 case 7:
  84.                     d.script[line] = str_dup(str);
  85.                     break;
  86.             }
  87.         }
  88.         if (oops)
  89.             break;
  90.                     /* sanity checking */
  91.         sprintf(token, "DIR_%d", line);
  92.         if (strcmp(temp_token, token)) {
  93.             sprintf(message, "is corrupted at line %d", line);
  94.             oops++;
  95.             break;
  96.         }
  97.     }
  98.     fclose(fp);
  99.  
  100.     if (oops) {
  101.         sprintf(buf, "Dialing directory file \"%s\"", d.d_path);
  102.         error_win(1, buf, message);
  103.     }
  104.     d.d_entries = line;
  105.                     /* if empty database */
  106.     if (!line) {
  107.         sprintf(buf, "Dialing directory file \"%s\"", d.d_path);
  108.         error_win(0, buf, "has no data");
  109.     }
  110.                     /* fill in the rest with defaults */
  111.     for (i=line+1; i<=NUM_DIR; i++) {
  112.         d.name[i] = null_ptr;
  113.         d.number[i] = null_ptr;
  114.         d.baud[i] = param->d_baud;
  115.         d.parity[i] = param->d_parity;
  116.         d.dbits[i] = param->d_dbits;
  117.         d.sbits[i] = param->d_sbits;
  118.         d.duplex[i] = *param->d_duplex;
  119.         d.script[i] = null_ptr;
  120.     }
  121.                     /* create an empty "manual" entry */
  122.     d.name[0] = null_ptr;
  123.     d.number[0] = null_ptr;
  124.     d.baud[0] = param->d_baud;
  125.     d.parity[0] = param->d_parity;
  126.     d.dbits[0] = param->d_dbits;
  127.     d.sbits[0] = param->d_sbits;
  128.     d.duplex[0] = *param->d_duplex;
  129.     d.script[0] = null_ptr;
  130.                     /* create an empty queue */
  131.     for (i=0; i<NUM_QUEUE; i++) {
  132.         d.q_ld[i] = '\0';
  133.         d.q_num[i] = -1;
  134.     }
  135.                     /* the start up d_cur is 0 */
  136.     d.d_cur = 0;
  137.     return(&d);
  138. }
  139.  
  140. /*
  141.  * Update a dialing directory entry.  Update only the one entry asked for,
  142.  * not the entire image in memory.  If the new entry is beyond the end of
  143.  * the physical file, then fill in the holes, and update "dir->d_entries".
  144.  * A non-zero return code means a non-fatal error.
  145.  */
  146.  
  147. int
  148. up_dir(entry)
  149. int entry;
  150. {
  151.     FILE *fp_in, *fp_out, *my_fopen();
  152.     int i;
  153.     char *temp[NUM_DIR+1], buf[200], *str_dup(), *str_rep();
  154.     void error_win(), free_ptr();
  155.  
  156.                     /* open for read */
  157.     if (!(fp_in = my_fopen(dir->d_path, "r"))) {
  158.         sprintf(buf, "\"%s\" for read", dir->d_path);
  159.         error_win(1, "Can't open dialing directory file", buf);
  160.     }
  161.                     /* read in a temporary version */
  162.     i = 0;
  163.     while (fgets(buf, 200, fp_in) != NULL)
  164.         temp[++i] = str_dup(buf);
  165.  
  166.     fclose(fp_in);
  167.                     /* alter only 1 entry */
  168.     sprintf(buf, "DIR_%d=%s;%s;%d-%c-%d-%d;%c;%s\n", entry,
  169.      dir->name[entry], dir->number[entry], dir->baud[entry],
  170.      dir->parity[entry], dir->dbits[entry], dir->sbits[entry],
  171.      dir->duplex[entry], dir->script[entry]);
  172.  
  173.     if (entry <= dir->d_entries)
  174.         temp[entry] = str_rep(temp[entry], buf);
  175.     else
  176.         temp[entry] = str_dup(buf);
  177.  
  178.                     /* fill in holes if beyond end */
  179.     if (entry > dir->d_entries+1) {
  180.         for (i=dir->d_entries+1; i<entry; i++) {
  181.             sprintf(buf, "DIR_%d=;;%d-%c-%d-%d;%c;\n", i,
  182.              param->d_baud, param->d_parity, param->d_dbits,
  183.              param->d_sbits, *param->d_duplex);
  184.             temp[i] = str_dup(buf);
  185.         }
  186.     }
  187.                     /* update "dir->d_entries" */
  188.     if (entry > dir->d_entries)
  189.         dir->d_entries = entry;
  190.  
  191.                     /* open for write */
  192.     if (!(fp_out = my_fopen(dir->d_path, "w"))) {
  193.         for (i=1; i<=dir->d_entries; i++)
  194.             free_ptr(temp[i]);
  195.         sprintf(buf, "\"%s\"", dir->d_path);
  196.         error_win(0, "No write permission on dialing directory file", buf);
  197.         return(1);
  198.     }
  199.                     /* put it back */
  200.     for (i=1; i<=dir->d_entries; i++) {
  201.         fputs(temp[i], fp_out);
  202.         free_ptr(temp[i]);
  203.     }
  204.  
  205.     fclose(fp_out);
  206.     return(0);
  207. }
  208.  
  209. /*
  210.  * Delete a range of dialing directory entries.  Actually, just copies
  211.  * default (empty) entries in place of deleted entries.  However, it will
  212.  * shrink the file if deletions occur at the physical EOF.  A non-zero
  213.  * return code means a non-fatal error.
  214.  */
  215.  
  216. int
  217. del_dir(first, last)
  218. int first, last;
  219. {
  220.     FILE *fp_in, *fp_out, *my_fopen();
  221.     int i;
  222.     char *temp[NUM_DIR+1], buf[200], *str_dup(), *str_rep();
  223.     void error_win(), free_ptr();
  224.                     /* sanity checking */
  225.     if (first > dir->d_entries)
  226.         return(0);
  227.     if (last > dir->d_entries)
  228.         last = dir->d_entries;
  229.  
  230.                     /* open for read */
  231.     if (!(fp_in = my_fopen(dir->d_path, "r"))) {
  232.         sprintf(buf, "\"%s\" for read", dir->d_path);
  233.         error_win(1, "Can't open dialing directory file", buf);
  234.     }
  235.                     /* read in a temporary version */
  236.     i = 0;
  237.     while (fgets(buf, 200, fp_in) != NULL)
  238.         temp[++i] = str_dup(buf);
  239.  
  240.     fclose(fp_in);
  241.                     /* delete the range of values */
  242.     for (i=first; i<=last; i++) {
  243.         sprintf(buf, "DIR_%d=;;%d-%c-%d-%d;%c;\n", i, param->d_baud,
  244.          param->d_parity, param->d_dbits, param->d_sbits,
  245.          *param->d_duplex);
  246.         temp[i] = str_rep(temp[i], buf);
  247.     }
  248.                     /* shrink the file? */
  249.     if (last >= dir->d_entries) {
  250.         for (i=first; i<=last; i++)
  251.             free_ptr(temp[i]);
  252.         dir->d_entries = first-1;
  253.     }
  254.                     /* open for write */
  255.     if (!(fp_out = my_fopen(dir->d_path, "w"))) {
  256.         for (i=1; i<=dir->d_entries; i++)
  257.             free_ptr(temp[i]);
  258.         sprintf(buf, "\"%s\"", dir->d_path);
  259.         error_win(0, "No write permission on dialing directory file", buf);
  260.         return(1);
  261.     }
  262.                     /* put it all back */
  263.     for (i=1; i<=dir->d_entries; i++) {
  264.         fputs(temp[i], fp_out);
  265.         free_ptr(temp[i]);
  266.     }
  267.  
  268.     fclose(fp_out);
  269.     return(0);
  270. }
  271.